package io.qar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.qar.commons.mybatis.service.impl.CrudServiceImpl;
import io.qar.commons.tools.redis.RedisUtils;
import io.qar.dao.BusFltInfoDao;
import io.qar.dao.BusFltRelationDao;
import io.qar.dto.BusFltInfoDTO;
import io.qar.entity.BusFltInfoEntity;
import io.qar.entity.BusFltRelationEntity;
import io.qar.entity.OriFocT2001Entity;
import io.qar.service.BusFltInfoService;
import io.qar.service.BusFltRelationService;
import io.qar.service.OriFocT2001Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 航班信息表
 *
 * @author yyyyjinying 
 * @since 3.0 2023-10-25
 */
@Service
public class BusFltInfoServiceImpl extends CrudServiceImpl<BusFltInfoDao, BusFltInfoEntity, BusFltInfoDTO> implements BusFltInfoService {
    @Autowired
    private OriFocT2001Service oriFocT2001Service;
    @Autowired
    private BusFltRelationService busFltRelationService;

    @Override
    public QueryWrapper<BusFltInfoEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<BusFltInfoEntity> wrapper = new QueryWrapper<>();
        return wrapper;
    }

    @Override
    public boolean insertBatchs(String beginDate, String endDate) {
        List<OriFocT2001Entity> list = oriFocT2001Service.getList(beginDate, endDate);

        // 1.批量删除
        List<Long> uuidlist = list.stream().map(item -> item.getUuid()).collect(Collectors.toList());
        // 批量修改关联信息
        busFltRelationService.addAndupdateBatchById(uuidlist, list);
        // 批量删除航班信息
        super.deleteBatchIds(uuidlist);

        // 2.批量增加
        List<BusFltInfoEntity> collect = list.stream().map(item -> {
            BusFltInfoEntity busFltInfoEntity = new BusFltInfoEntity();
            busFltInfoEntity.setFltDate(item.getFlightDate());
            busFltInfoEntity.setFltNo(item.getFlightNo());
            busFltInfoEntity.setAcReg(item.getMachineNo());
            busFltInfoEntity.setDepApt_4(item.getTakeoffStation());
            busFltInfoEntity.setArrApt_4(item.getLandStation());
            busFltInfoEntity.setArrApt_3(null);
            busFltInfoEntity.setDepApt_3(null);
            busFltInfoEntity.setCreateTime(new Date());
            busFltInfoEntity.setDepTime(item.getActualTakeoffTime());
            busFltInfoEntity.setArrTime(item.getActualArrivalTime());
            busFltInfoEntity.setFltType(item.getFlighttype());
            busFltInfoEntity.setDOrI(item.getFlightnature());
            busFltInfoEntity.setFltId(item.getUuid());
            busFltInfoEntity.setFlgVr1(item.getAltnReturnFlightFlag1());
            return busFltInfoEntity;
        }).collect(Collectors.toList());
        super.insertBatch(collect);

        return true;
    }
}